﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

// qt headers
#include <QSplitter>
#include <QFileSystemModel>
#include <QTreeView>
#include <QTimer>
#include <QJsonObject>

// gcmp headers
#include "IDocument.h"

// custom headers
#include "UIService.h"
#include "GcsMainWindow.h"
#include "BnufBulletin.h"
#include "UserService.h"
#include "PermissionWatcher.h"
#include "BaseCommonUtils.h"
#include "GbmpNew.h"
#include "GmepUIWindowUtils.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 
namespace gmepgcs
{
    UIService::UIService(QObject *parent /*= nullptr*/) :
        QObject(parent),
        m_spMainWindow(nullptr)
    {
    }

    UIService::~UIService()
    {
    }
    
    UIService * UIService::s_pInstance = nullptr;
    UIService * UIService::Get()
    {
        if (!UIService::s_pInstance)
        {
            UIService::s_pInstance = NEW_AS_QT(UIService);
        }
        return UIService::s_pInstance;
    }

    void UIService::ShowMainWindow()
    {
        if (!m_spMainWindow)
        {
            InitMainWindow();
        }
        if (m_spMainWindow)
        {
            m_spMainWindow->setParent(gmep::GetMainWindow());

            m_spMainWindow->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
            m_spMainWindow->show();
        }
                
    }

    void UIService::HideMainWindow()
    {
        if (!m_spMainWindow)
        {
            return;
        }

        m_spMainWindow->hide();
    }

    MainWindow * UIService::GetMainWindow()
    {
        return m_spMainWindow.get();
    }

    bool UIService::IsMainWindowInitialized()
    {
        return m_spMainWindow != nullptr;
    }

    void UIService::SetMainWindowState(const Qt::WindowState & windowstate)
    {
        if (!m_spMainWindow)
        {
            return;
        }

        if (!m_spMainWindow->isVisible())
        {
            return;
        }

        if (m_spMainWindow->windowState() == windowstate)
        {
            return;
        }

        m_spMainWindow->setWindowState(windowstate);
    }

    void UIService::InitMainWindow()
    {
        m_spMainWindow = std::make_shared<MainWindow>();
        RefreshUIpermission();

        BnufBulletin::SetDock(m_spMainWindow.get());
    }

    void UIService::RefreshCurrentPage(bool isCollaborationDocument)
    {
        if (!m_spMainWindow)
        {
            return;
        }

        m_spMainWindow->RefreshCurrentPage(isCollaborationDocument);
    }

    void UIService::RefreshUIpermission()
    {
        if (!m_spMainWindow)
        {
            return;
        }

        PermissionWatcher::Get()->CheckPermission();
    }



    void UIService::GotoProjectPage()
    {
        if (!m_spMainWindow)
        {
            return;
        }

        m_spMainWindow->GotoProjectPage();
    }    

    void UIService::RefreshProjectPage()
    {
        if (!m_spMainWindow)
        {
            return;
        }

        m_spMainWindow->RefreshProjectPage();
    }
    
    void UIService::SetIsByEditComponentShow(const bool& isByEditCommand)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_spMainWindow, L"m_spMainWindow is nullptr", L"GMEP", L"2024-01-30");
        m_spMainWindow->SetIsByEditComponentShow(isByEditCommand);
    }

    void UIService::ShowProjectComponentEditingPage(const std::wstring &projectId, const std::wstring &enterpriseId, const std::wstring &componentId, const int32_t &componentVersion, const std::wstring &componentName, const std::wstring &componentFamilyTypeName)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_spMainWindow, L"m_spMainWindow is nullptr", L"GMEP", L"2024-01-30");
        m_spMainWindow->ShowProjectComponentEditingPage(projectId, enterpriseId, componentId, componentVersion, componentName);
        m_spMainWindow->SetProjectComponentEditingPageCurrentFamilyTypeName(componentFamilyTypeName);
    }

}